# Please install OpenAI SDK first: `pip3 install openai`
from openai import OpenAI

from django.http import StreamingHttpResponse



class Qwen:
    def __init__(self, host, model, gen_kwargs):
        self.client = OpenAI(
            api_key="EMPTY",  # APIKey
            base_url=host + "/v1",  # 请求地址  /chat/completions
        )
        # 二：设置生成参数和输入消息
        self.gen_kwargs = gen_kwargs
        # 模型
        self.model = model

    def inference(self, messages, stream=False):
        # 面向对象的模块化编程
        response = self.client.chat.completions.create(
            model=self.model,
            messages=messages,
            stream=stream,
            **self.gen_kwargs,
        )
        # 非流式的实现
        # 注意：只是非流式的实现
        if not stream: # 非流式
            return response.choices[0].message.content

        return response
